/*
 * @(#)SecurityCheckingFilter.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.security.filter;

import java.io.IOException;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.bis.common.entity.SessionEntity;
import com.bis.framework.security.context.SecurityContextInfo;
import com.bis.framework.util.common.COConstants;
import com.bis.framework.util.common.SystemConfig;

/**
 * SecurityCheckingFilter
 * 
 * @author BIS 2012-12-24
 */
public class SecurityCheckingFilter extends BaseFilter
{

    /**
     * Logger
     */
    private Logger logger = Logger.getLogger(getClass());
    
    /**
     * doPostProcessing
     */
    @Override
    public void doPostProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException
    {
    }

    /**
     * 系统过滤器。
     */
    @Override
    public boolean doPreProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException
    {
        boolean bReturn = true;
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String requestPath = httpRequest.getServletPath();

        SessionEntity sessionEntity = (SessionEntity) httpRequest.getSession().getAttribute(SecurityContextInfo.SESSION_CONTEXT_KEY);

        if (null == sessionEntity)
        {
//            if (!excludeUrl.contains(requestPath))
//            {
//                bReturn = false;
//                redirectPage(request, response, redirectUrl);
//            }
        }
        return bReturn;
    }

    /**
     * initParameter
     */
    @Override
    public void initParameter(FilterConfig filterConfig) throws ServletException
    {
        redirectUrl = filterConfig.getInitParameter("redirectUrl");
        excludeUrl = filterConfig.getInitParameter("excludeUrl");
        errorUrl = filterConfig.getInitParameter("errorUrl");
        logger.info("SecurityCheckingFilter initParameter ok!");
    }

    /**
     * redirectPage
     */
    @Override
    public void redirectPage(ServletRequest request, ServletResponse response, String redirectUrl) throws IOException
    {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.sendRedirect(httpRequest.getContextPath() + redirectUrl);
    }
}
